package io.awen.modules.sign.controller;

import io.awen.common.utils.PageUtils;
import io.awen.common.utils.R;
import io.awen.modules.sign.entity.SignCarEntity;
import io.awen.modules.sign.entity.SignCarLonLatEntity;
import io.awen.modules.sign.qo.SignCarModel;
import io.awen.modules.sign.qo.SignCarQo;
import io.awen.modules.sign.service.SignCarService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 收运单位车辆信息表
 *
 * @author liu.wen
 * @email liu.wen@qq.com
 * @date 2022-08-10 00:47:08
 */
@RestController
@RequestMapping("sign/signcar")
public class SignCarController {
    @Autowired
    private SignCarService signCarService;

    /**
     * 按条件查询 车辆 总数
     * @param qo
     * @return
     */
    @PostMapping("/total")
    public R total(@RequestBody SignCarQo qo){
        int total = signCarService.total(qo);
        return R.ok().put("total", total);
    }

    /**
     * 更新车辆经纬度与速度
     * @param id
     * @param lon
     * @param lat
     * @param speed
     * @param dataTime
     * @return
     */
//    @ApiOperation("更新车辆经纬度与速度")
//    @PostMapping("/updateLonLat")
//    public R updateLonLat(Long id, Double lon, Double lat, Double speed, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date dataTime) {
//        signCarService.updateLonLat(id, lon, lat, speed, dataTime);
//        return R.ok();
//    }

    /**
     * 车实时值
     */
    @PostMapping("/realList")
    public R realList() {
        return R.ok().put("list", signCarService.getCarMap().values());
    }

    /**
     * 车实时值。通过车牌号过滤车辆
     */
    @RequestMapping("/realListByQo")
//    @RequiresPermissions("sign:signcar:list")
    public R realListByQo(@RequestBody SignCarQo qo){
        List<SignCarEntity> list = signCarService.realListByQo(qo);

        return R.ok().put("list", list);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody SignCarQo qo){
        PageUtils page = signCarService.queryPage(qo);

        return R.ok().put("page", page);
    }

    /**
     * jtt808获取在线车数量
     * @return
     */
    @RequestMapping("/getCarInfoFromJtt808")
    public R getCarInfoFromJtt808(){
        List<SignCarEntity> list = signCarService.getCarInfoFromJtt808();
        return R.ok().put("list", list);
    }

    /**
     * 获取今日上线车数量和离线车数量
     * @return
     */
    @RequestMapping("/getCarCountToday")
    public R getCarCountToday(){
        SignCarModel carCountToday = signCarService.getCarCountToday();
        return R.ok().put("data", carCountToday);
    }



    @RequestMapping("/getCarBasicInfo/{id}")
//    @RequiresPermissions("sign:signcar:info")
    public R getCarBasicInfo(@PathVariable("id") Long id){
        SignCarEntity signCar = signCarService.getById(id);
        return R.ok().put("signCar", signCar);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sign:signcar:info")
    public R info(@PathVariable("id") Long id){
		SignCarEntity signCar = signCarService.findCar(id);
        return R.ok().put("signCar", signCar);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoByDriver/{userId}")
    public R infoByDriver(@PathVariable("userId") Long userId){
        SignCarEntity signCar = signCarService.infoByDriver(userId);

        return R.ok().put("signCar", signCar);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sign:signcar:save")
    public R save(@RequestBody SignCarEntity signCar){
		signCarService.saveAndCache(signCar);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
//    @RequiresPermissions("sign:signcar:update")
    public R update(@RequestBody SignCarEntity signCar){
		signCarService.updateAndCache(signCar);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sign:signcar:delete")
    public R delete(@RequestBody Long[] ids){
		signCarService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
